var ctx;

function setCtx(ctx){
	this.ctx = ctx;
}

$(function(){
	$.post(ctx+"/orderReport/orderEchartsShow?timestamp=" + Date.parse(new Date()),
			{days:7,type:1},function(data){
				data = JSON.parse(data);
				showChart(data);
				$("#beginTime").val(data.obj[0][0][0]);
				$("#endTime").val(data.obj[0][0][6]);
	})
	
})

function viewCharts(days){
	$.post(ctx+"/orderReport/orderEchartsShow",
			{days:days,type:1},function(data){
				data = JSON.parse(data);
				showChart(data);
				$("#beginTime").val(data.obj[0][0][0]);
				$("#endTime").val(data.obj[0][0][days-1]);
	})
}

function viewYearCharts(){
	$.post(ctx+"/orderReport/orderMonthEchartsShow",
			{secType:1,monthSum:12},function(data){
				data = JSON.parse(data);
				showChart(data);
				$("#beginTime").val(data.obj[0][0][0]);
				$("#endTime").val(data.obj[0][0][11]);
	})
}

function showChart(target){
	var dates = [];
	for(var i = 0; i<target.obj[0][0].length;i++){
		dates.push(target.obj[0][0][i].substr(5));
	}
	var myChart = echarts.init(document.getElementById('echartsShow'));
	var option = {
		    tooltip: {
		        trigger: 'axis'
		    },
		    legend: {
		        data:['已完成订单','已取消订单']
		    },
		    grid: {
		        left: '3%',
		        right: '4%',
		        bottom: '3%',
		        containLabel: true
		    },
		    xAxis: {
		        type: 'category',
		        boundaryGap: false,
		        data: dates
		    },
		    yAxis: {
		        type: 'value'
		    },
		    series: [
		        {
		            name:'已完成订单',
		            type:'line',
		            stack: '总量',
		            itemStyle : {  
                        normal : {  
                        	color:'#32d2c6',
                            lineStyle:{  
                                color:'#32d2c6'  
                            }  
                        }  
                    },  
		            data:target.obj[1][0]
		        },
		        {
		            name:'已取消订单',
		            type:'line',
		            stack: '总量',
		            itemStyle : {  
                        normal : { 
                        	color:'#f8a20f',
                            lineStyle:{  
                                color:'#f8a20f'  
                            }  
                        }  
                    },  
		            data:target.obj[2][0]
		        }
		    ]
		};


	myChart.setOption(option);
}

function search(){
	var beginTime = $("#beginTime").val();
	var beginDate = new Date(beginTime);
	var endTime = $("#endTime").val();
	var endDate = new Date(endTime);
	var num = (endDate-beginDate)/(1000*3600*24);
	var days = parseInt(Math.ceil(num))+1;
	if(days == 0){
		days = 1;
	}
	if(days >= 0 && days <= 90){
		$.post(ctx+"/orderReport/orderEchartsShow",{
			beginDate:beginTime,
			endDate:endTime,
			days:days,
			type:2
		},function(data){
			data = JSON.parse(data);
			showChart(data);
		})
	} else {
		viewMonthCharts();
	}
}

function viewMonthCharts(){
	var beginTime = $("#beginTime").val();
	var beginDate = new Date(beginTime);
	var endTime = $("#endTime").val();
	var endDate = new Date(endTime);
	var num = (endDate-beginDate)/(1000*3600*24*30);
	var monthSum = parseInt(num)+1;
	if(monthSum == 0){
		monthSum = 1;
	}
	$.post(ctx+"/orderReport/orderMonthEchartsShow",
			{secType:2,monthSum:monthSum,beginDate:beginTime,
		     endDate:endTime},function(data){
		    	 data = JSON.parse(data);
				showChart(data);
	})
}